package pro.cherish.knowledgedemoasync.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import pro.cherish.knowledgedemoasync.t2.TestReturn;
import pro.cherish.knowledgedemoasync.t2.Tmp;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * @author 创建人：cherish
 * @version 版本号：V1.0
 * <p>
 * ***************************修订记录************************************
 * 2019-10-27 cherish 创建该类功能。
 * **********************************************************************
 * </p>
 * @ClassName 类名：TestController
 * @Description 功能说明：
 * <p>
 * TODO
 * </p>
 * **********************************************************************
 * @date 创建日期： 2019-10-27 cherish
 */
@RestController
@Slf4j
public class TestController {

    @Autowired
    private Tmp tmp;

    @Autowired
    private TestReturn testReturn;

    /**
     * 这个web测试和spring boot test测试中 TestResult类中的测试是比对测试：在spirng boot test 环境中是需要tmp过渡一下的，但是在web环境中是两者都有异步效果
     * @param msg
     * @throws InterruptedException
     * @throws ExecutionException
     */
    @RequestMapping("/testAsync")
    public void testAsync(String msg) throws InterruptedException, ExecutionException {
        // Future<Object> future = testReturn.getSomething(msg);
        Future<Object> future = this.tmp.tmp(msg);
        Object data;
        while (true) {
            if (future.isDone()) {
                data = future.get();
                break;
            }
            log.info("主线程还在等待哦，一秒后我再问");
            Thread.sleep(1000);
        }
        log.info("get 异步任务返回值{}", data);
    }
}
